<template>
  <div class="detail">
    <!-- 头部导航 -->
    <navbar>
      <template #left>
        <div @click="toLeft">
          <i class="van-icon van-icon-arrow-left"></i>
        </div>
      </template>
      <template #center>
        <h3 class="title1">
          {{ detailObj.title }}
        </h3>
      </template>
      <template #right>
        <div @click="toShare">评论区</div>
      </template>
    </navbar>
    <!-- 文章标题 -->
    <div class="title">
      <h1>{{ detailObj.title }}</h1>
    </div>
    <!-- 作者信息 -->
    <div class="author">
      <div class="avatar">
        <img :src="detailObj.aut_photo" alt="" />
      </div>
      <div class="msg">
        <div class="name">{{ detailObj.aut_name }}</div>
        <div class="time">{{ detailObj.pubdate | dayjsFilter }}</div>
      </div>
      <div class="follow">
        <van-button
          type="info"
          icon="plus"
          v-if="!is_followed"
          @click="isFollowing"
          >关注</van-button
        >
        <van-button type="info" v-else @click="offFollowing">已关注</van-button>
      </div>
    </div>
    <!-- 文章内容 -->
    <div class="content">
      <div v-html="detailObj.content" class="txt"></div>
    </div>
    <!-- 点赞模块 -->
    <div class="like">
      <van-button
        type="default"
        round
        icon="like"
        @click="isLikings"
        :class="{ color: attitude === 1 }"
        >点赞</van-button
      >
      <van-button
        type="default"
        round
        icon="delete-o"
        @click="isLikings2"
        :class="{ color: attitude === 0 }"
        >不喜欢</van-button
      >
    </div>
    <!-- 猜你喜欢 -->
    <div class="maybe">
      <h4>猜你喜欢</h4>
    </div>
    <div ref="comment">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        style="padding-bottom:50px"
      >
        <!-- 评论列表 -->
        <div class="comment" v-for="(item, index) in commentList" :key="index">
          <div class="top">
            <div class="img">
              <img :src="item.aut_photo" alt="" />
            </div>
            <div class="msg">
              <div class="name">{{ item.aut_name }}</div>
            </div>
            <div class="star">
              <van-icon name="like" color="red" size="25"> </van-icon>
              <!-- 评论点赞 -->
              <span>{{ item.like_count }}</span>
            </div>
          </div>
          <div class="bottom">
            <div class="text">{{ item.content }}</div>
            <div class="time">
              {{ item.pubdate | dayjsFilter }}
              <span
                @click="
                  openReply(
                    item.com_id.c[0].toString() + item.com_id.c[1].toString(),
                    index
                  )
                "
              >
                <i class="txt2">回复</i>{{ item.reply_count }}
              </span>
            </div>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 底部发表评论区域 -->
    <div class="footer">
      <div class="left">
        <input
          type="text"
          placeholder="写评论"
          v-model="msg"
          @keyup.enter="getMsg"
        />
      </div>
      <div class="right">
        <div class="item">
          <span>{{ total_count }}</span>
          <van-icon name="comment-o" />
        </div>
        <div class="item">
          <van-icon
            v-if="is_collected"
            @click="cancelCollect"
            class="active"
            name="star-o"
            collectAc
          />
          <van-icon v-else @click="collectAc" name="star-o" />
        </div>
        <div class="item">
          <van-icon name="share-o" />
        </div>
      </div>
    </div>
    <!-- 开启回复面板 -->
    <van-popup v-model="show" position="bottom" :style="{ height: '60%' }">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad1"
        style="padding-bottom:50px"
      >
        <div class="header">
          <van-icon name="cross" size="30" />
          <span class="mytitle">{{ total_count1 }}条回复</span>
        </div>
        <div class="comment" v-for="(item, index) in commentList1" :key="index">
          <div class="top">
            <div class="img">
              <img :src="item.aut_photo" alt="" />
            </div>
            <div class="msg">
              <div class="name">{{ item.aut_name }}</div>
            </div>
            <div class="star">
              <van-icon name="like" color="red" size="25"> </van-icon>
              <!-- 评论点赞 -->
              <span>{{ item.like_count }}</span>
            </div>
          </div>
          <div class="bottom">
            <div class="text">{{ item.content }}</div>
            <div class="time">
              {{ item.pubdate | dayjsFilter }}
            </div>
          </div>
        </div>
        <div class="footer">
          <div class="left">
            <input
              type="text"
              placeholder="回复"
              style="width: 249px;"
              v-model="msg1"
              @keyup.enter="setMessage(idone)"
            />
          </div>
          <div class="right">
            <div class="item">
              <span>{{ total_count }}</span>
              <van-icon name="comment-o" style="margin-right:10px" />
            </div>
          </div>
        </div>
      </van-list>
    </van-popup>
    <back-top :distance="1000" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {
  getDetailApi,
  getCommentApi,
  getFollowingsApi,
  getFollowOffApi,
  getLikingsApi,
  getOffLikingsApi,
  getDislikesApi,
  getOffDislikesApi,
  setCommentApi,
  cancelCollectApi,
  collectApi
} from '../../api/detail'
import navbar from '../../components/navBar'
export default {
  name: 'comment',
  data () {
    return {
      currentReply: '',
      // 文章id
      id: '',
      detailObj: {}, // 文章详情
      commentList: [], // 评论列表
      loading: false, // 数据是否更新完成
      finished: false, // 上拉刷新是否完成状态
      show: false, // 控制回复面板的控制和隐藏
      // 显示容量
      limit: 10,
      // 开始容量id
      offset: 0,
      flag: true, // 是否关注
      // 所有评论或回复的最后一个id
      end_id: '',
      // 本次返回结果的最后一个评论id
      last_id: '',
      // 评论或回复的内容
      results: '',
      // 动态点赞和不喜欢状态
      // -1 无状态 0 不喜欢 1 点赞
      attitude: -1,
      // 总数
      total_count: 0,
      // 文章评论内容
      msg: '',
      // 当前作者文章评论
      userMsg: [],
      // 开始评论id
      last_id1: '',
      // 结束的id
      end_id1: '',
      // 显示容量
      limit1: 10,
      // 开始容量id
      offset1: 0,
      // 回复评论列表
      commentList1: [],
      // 总数
      total_count1: '',
      // 回复评论
      msg1: '',
      userMsg1: '',
      idone: '',
      is_followed: '',
      is_collected: '',
      offsetTop2: ''
    }
  },
  async created () {
    // 获取文章id
    this.id = this.$route.params.artid
    // 获取文章详情
    const res = await getDetailApi(this.id)
    console.log(res)
    this.detailObj = res.data
    this.is_followed = res.data.is_followed
    this.is_collected = res.data.is_collected
    this.collect = res.data.is_collected
    // 进入页面获取到动态的状态
    this.attitude = this.detailObj.attitude
    console.log(this.detailObj)
  },
  mounted () {
    setTimeout(() => {
      this.offsetTop2 = this.$refs.comment.offsetTop - 200
    }, 3000)
  },
  methods: {
    // 点击对文章点赞
    async isLikings () {
      // -1 和 0 时候可以使用点赞功能
      if (this.attitude === -1 || this.attitude === 0) {
        const res = await getLikingsApi({
          target: this.id
        })
        if (res.message === 'OK') {
          // 已经成功点赞
          this.attitude = 1
          this.$toast.success('点赞成功')
        }
      } else if (this.attitude === 1) {
        // 已经成功取消点赞
        this.attitude = -1
        try {
          const res = await getOffLikingsApi(this.id)
          console.log(res)
        } catch (err) {}
      }
    },
    // 不喜欢按钮
    async isLikings2 () {
      // -1 和 1 时候可以使用不喜欢功能
      if (this.attitude === -1 || this.attitude === 1) {
        const res = await getDislikesApi({
          target: this.id
        })
        if (res.message === 'OK') {
          // 已经成功不喜欢
          this.attitude = 0
        }
      } else if (this.attitude === 0) {
        // 已经成功取消不喜欢
        this.attitude = -1
        try {
          const res = await getOffDislikesApi(this.id)
          console.log(res)
        } catch (err) {}
      }
    },
    // 点击关注功能
    async isFollowing () {
      this.$toast.loading('加载中')
      const res = await getFollowingsApi({
        target: this.detailObj.aut_id
      })
      if (res.message === 'OK') {
        const res1 = await getDetailApi(this.id)
        this.is_followed = res1.data.is_followed
        console.log(res1)
        this.$toast.success('关注成功')
      }
    },
    // 点击取消关注
    async offFollowing () {
      try {
        this.$toast.loading('加载中')
        const res = await getFollowOffApi(this.detailObj.aut_id)
        console.log(res)
      } catch (err) {
        const res1 = await getDetailApi(this.id)
        console.log(res1)
        this.is_followed = res1.data.is_followed
        this.$toast.success('取消关注成功')
        console.log(err)
      }
    },
    // 获取文章评论下拉刷新
    async onLoad () {
      if (!this.last_id) {
        // 进入页面就发送一次请求获取页面回复内容
        const res = await getCommentApi({
          type: 'a',
          source: this.id,
          limit: this.limit
        })
        console.log(res)

        // 文章评论的总id
        this.end_id = res.data.end_id
        // 每次结束的最后一个id
        this.last_id = res.data.last_id
        // 遍历对象数组
        this.commentList.push(...res.data.results)
        // 修改状态为false
        this.loading = false
        // 总数
        this.total_count = res.data.total_count
        if (this.commentList.length >= this.total_count) {
          this.finished = true
        }
        console.log(res, 2222)
      } else {
        // 进入页面就发送一次请求获取页面回复内容
        const res = await getCommentApi({
          type: 'a',
          source: this.id,
          offset: this.last_id,
          limit: this.limit
        })
        // 重新赋值最后的id
        this.last_id = res.data.last_id
        // 文章评论的总id
        this.end_id = res.data.end_id
        this.commentList.push(...res.data.results)
        // 修改状态为false
        this.loading = false
        // 判断是否到达总id
        this.total_count = res.data.total_count
        if (this.commentList.length >= this.total_count) {
          this.finished = true
        }
        console.log(res)
      }
    },
    // 跳转主页
    toLeft () {
      this.$router.go(-1)
    },
    // 跳转至分享
    toShare () {
      console.log(this.offsetTop2)
      window.scrollTo(0, this.offsetTop2)
    },
    // 获取评论的回复内容
    async onLoad1 () {
      // 发送请求获取获取当前文章id
      if (!this.last_id1) {
        // 进入页面就发送一次请求获取页面回复内容
        console.log(22222)
        const res = await getCommentApi({
          type: 'c',
          source: this.idone,
          limit: this.limit1
        })
        console.log(res)
        // 文章评论的总id
        this.end_id1 = res.data.end_id
        // 每次结束的最后一个id
        this.last_id1 = res.data.last_id
        // 遍历对象数组
        this.commentList1.push(...res.data.results)
        // 修改状态为false
        this.loading = false
        // 总数
        this.total_count1 = res.data.total_count
        if (this.commentList1.length >= this.total_count1) {
          this.finished = true
        }
        console.log(res.data)
      } else {
        // 进入页面就发送一次请求获取页面回复内容
        const res = await getCommentApi({
          type: 'c',
          source: this.idone,
          offset: this.last_id1,
          limit: this.limit1
        })
        console.log(res)
        // 重新赋值最后的id
        this.last_id1 = res.data.last_id
        // 文章评论的总id
        this.end_id1 = res.data.end_id
        this.commentList1.push(...res.data.results)
        // 修改状态为false
        this.loading = false
        // 判断是否到达总id
        this.total_count1 = res.data.total_count
        if (this.commentList1.length >= this.total_count1) {
          this.finished = true
        }
        console.log(res.data)
      }
    },
    // 展开回复面板 && 分享
    openReply (id, index) {
      this.currentReply = index
      this.finished = false
      // 文章评论的总id
      this.end_id1 = ''
      // // 每次结束的最后一个id
      this.last_id1 = ''
      this.commentList1 = []
      this.show = true
      this.idone = id
    },
    // 文章评论点赞功能
    // 文章发表评论
    async getMsg () {
      const res = await setCommentApi({
        target: this.id,
        content: this.msg
      })
      console.log(res)
      this.finished = false
      // 要动态渲染数据
      // this.userMsg = JSON.parse(JSON.stringify(this.commentList[0]))
      // this.userMsg.content = this.msg
      // this.commentList.unshift(this.userMsg)
      // 清空文章评论
      this.msg = ''
      this.commentList = []
      this.last_id = ''
      window.scrollTo(0, this.offsetTop2)
      this.$toast.success('评论成功')
    },
    // 当前当个文章的评论回复
    async setMessage (id) {
      const res = await setCommentApi({
        art_id: this.id,
        target: id,
        content: this.msg1
      })

      // 要动态渲染数据
      this.total_count1++
      this.commentList[this.currentReply].reply_count++
      console.log(this.total_count)
      this.commentList1.unshift(res.data.new_obj)
      // 清空文章评论
      this.msg1 = ''
    },
    // 收藏文章
    async collectAc () {
      this.$toast.loading('加载中')
      const res = await collectApi({
        target: this.detailObj.art_id
      })
      console.log(res)
      const res1 = await getDetailApi(this.id)
      this.is_collected = res1.data.is_collected
      this.$toast.success('收藏成功')
    },
    async cancelCollect () {
      try {
        this.$toast.loading('加载中')
        const res = await cancelCollectApi(this.detailObj.art_id)
        console.log(res)
      } catch (err) {
        const res1 = await getDetailApi(this.id)
        this.is_collected = res1.data.is_collected
        this.$toast.success('取消收藏成功')
      }
    }
  },
  components: {
    navbar
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less" scoped>
.color {
  color: red !important;
}
.detail {
  overflow: hidden;
  /deep/ .title1 {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    font-size: 16px;
  }
  .title {
    padding: 20px 0;
    h1 {
      text-align: center;
      font-weight: normal;
    }
  }
  margin-top: 46px;
  font-size: 16px;
  .author {
    align-items: center;
    display: flex;
    .avatar {
      img {
        width: 40px;
        height: 40px;
        margin: 0 10px;
        border-radius: 50%;
      }
    }
    .msg {
      flex: 1;
    }
    .follow {
      margin-right: 10px;
    }
  }
  .content {
    padding: 30px 10px;
    width: 100%;
    box-sizing: border-box;
    /deep/ .txt {
      pre {
        border: 1px solid #d1d1d1;
        line-height: 190%;
        margin: 0 0 1.75em;
        max-width: 100%;
        overflow: auto;
        padding: 15px;
        white-space: pre;
        white-space: pre-wrap;
        word-wrap: break-word;
        code {
          background: #f1f1f1;
          color: #555;
          margin: 0 2px;
          padding: 2px 4px;
          border-radius: 2px;
        }
      }
      .com-widget-operations {
        height: 0;
      }
      img {
        width: 100%;
      }
      .image-container {
        max-height: 200000px !important
        ;
      }
    }
  }
  .like {
    display: flex;
    justify-content: space-evenly;
  }
  .maybe {
    h4 {
      padding-left: 10px;
    }
  }
  .comment {
    padding: 20px;
    .top {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
        margin-right: 10px;
        border-radius: 50%;
      }
    }
    .msg {
      flex: 1;
      .name {
        color: #7492c4;
        margin-bottom: 10px;
      }
    }
    .star {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      span {
        margin-left: 5px;
        margin-top: 10px;
      }
    }
    .bottom {
      margin-left: 50px;
      .text {
        margin-bottom: 10px;
      }
      span {
        padding: 5px 18px;
        background-color: #eff7f8;
        border-radius: 40px;
        margin-left: 10px;
        font-size: 14px;
        .txt2 {
          font-style: normal;
        }
      }
    }
  }
  .footer {
    position: fixed;
    width: 100%;
    height: 50px;
    left: 0;
    bottom: -5px;
    background-color: #fff;
    border-top: 1px solid #cfcfcf;
    display: flex;
    .left {
      flex: 1;
      padding-left: 20px;
      padding-top: 8px;
      input {
        height: 30px;
        padding-left: 20px;
        border-radius: 30px;
        border: 1px solid #eee;
        outline: none;
      }
    }
    .right {
      display: flex;
      align-items: center;
      padding-right: 20px;
      .item {
        position: relative;
        font-size: 20px;
        margin-left: 20px;
        margin-top: 10px;
        span {
          position: absolute;
          left: 50%;
          top: -10px;
          width: 20px;
          height: 18px;
          line-height: 18px;
          text-align: center;
          color: #fff;
          font-size: 12px;
          display: inline-block;
          background-color: #e22829;
          border-radius: 50%;
          z-index: 1;
        }
      }
    }
  }
  /deep/ .header {
    display: flex;
    padding: 5px;
    /deep/ .mytitle {
      text-align: center;
      flex: 1;
      font-size: 20px;
    }
  }
}
.active {
  color: red;
  van-icon {
    background-color: red;
  }
}
</style>
